import pygame
import sys
import random

pygame.init()
size = width, height= (600, 600)
bg_color = (89, 230, 180)
poses = [[300, 300, -1,  random.randint(1, 5)],[300, 300, -1,  random.randint(1, 5)],[300, 300, -1,  random.randint(1, 5)]]

pygame.display.set_caption("ball game")
screen = pygame.display.set_mode(size)

clock = pygame.time.Clock()
while 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
    screen.fill(bg_color)
    for pos in poses:
        pygame.draw.circle(screen, (255,0,0), (pos[0], pos[1]), 20)
        pos[1] += pos[2]
        pos[0] += pos[3]
        if pos[1] >= height-20:
            pos[2] = -pos[2]
        elif pos[1] <= 20:
            pos[2] = -pos[2]

        if pos[0] <=0:
            pos[3] = random.randint(1, 5)
        elif pos[0] >= 580:
            pos[3] = -random.randint(1, 5)
    clock.tick(100)
    pygame.display.update()
